<template>
	<view class="container">
		<image class="logo" src="../../static/img/logo.png" mode="widthFix"></image>
		<view class="line"></view>
		<view class="bll">
			<view class="black">申请获取以下权限：</view>
			<view class="select">获取你的公开信息（昵称、头像、地区等）</view>
			<view class="buttn">
				<button class="GoBack" @click="GoBack">暂不登录</button>
				<button class="Gologin" open-type="getUserInfo" @getuserinfo="getPhoneNumber">授权登录</button>
			</view>

			<checkbox-group @change="checkboxChange">
				<view class="uni-list-cell">
					<label>
						<checkbox style="transform: scale(0.8)" class="round red"></checkbox>
					</label>
					<view>登录即代表同意<text class="blue" @click="goMyPrivacy">《隐私政策》</text>、<text class="blue"
							@click="goMyAgreement">《用户协议》</text></view>
				</view>
			</checkbox-group>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: false, //勾选同意协议
				org: 0, //主体  默认是个人用户   1是商家用户
				sessionId: '',
				encryptedData: '',
				iv: ''
			}
		},
		methods: {
			getPhoneNumber(e) {
				var _this = this 
				
				if (!_this.checked) { //	1.如果没有勾选协议将退出，提示用户先登录
					uni.showToast({
						title: '请先勾选用户协议！',
						duration: 2000,
						icon: 'error'
					})
					return
				}

				uni.showModal({
					title: '请选择',
					content: '登录小程序的主体',
					cancelText: '商家',
					confirmText: '个人',
					success: function(res) {
						if (res.confirm) {
							_this.org = 0
							
						} else if (res.cancel) {
							_this.org = 1
							
						}
						
						wx.getUserProfile({
							desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
							success: (resp) => {

								uni.login({
									success(res) {

										if (res.code) {
											//发起网络请求

											_this.requestfs({
												url: '/user/getSessionId/' + res.code,
												data:{},
											}).then(rr=>{
													
													if(rr.data.msg=="操作成功"){
														
													_this.sessionId = rr.data.data.sessionId

													let wxAuth = {
														encryptedData: e.detail.encryptedData,
														iv: e.detail.iv,
														sessionId: _this.sessionId,
														org: _this.org
													}
													
													console.log(11,wxAuth.org)
													
													if (e.detail.errMsg =="getUserInfo:ok") { // 用户允许或去手机号	
																												
														_this.requestfs({
															url: "/user/authLogin",
															data: wxAuth,
															method: "POST"
														}).then(ress => {
															
															if(ress.data.msg=="登录成功"){ 
															// if(ress.data.msg=="登录成功" &&  ress.data.data.token){ 
															// var refresh=false
															// _this.requestfs({
															// 	url: "/user/userInfo?refresh="+refresh,
																
															// }).then(rpp => {
																																
																uni.setStorage({ //缓存机制----后端需要的
																	key: 'UserInformation',
																	data: {
																		usertoken: ress.data.data.token,
																		avatarUrl:ress.data.data.avatarUrl,
																		nickName: ress.data.data.nickname,
																		org: _this.org
																		// bh:rpp.data.data.bh
																	},
																	success: function(success) {
																		
																	}
																})
																																																
																uni.showToast({
																	title: '登录成功！',
																	duration: 2000,
																	icon: 'none'
																})
																	
																uni.navigateBack(1)
																//1111
															// })
																
															}
															//22
														})
													}
												}
												
												
											})
										} else {
											console.log('登录失败！' + res.errMsg)
										}
									}
								})
							}
						})

					}
				});


			},
			checkboxChange: function(e) { //监听勾选框变化			
				if (e.target.value.length > 0) {
					this.checked = true
				} else {
					this.checked = false
				}
			},
			GoBack() {
				uni.navigateBack(1)
			},
			Gologin() {},
			PersonOrShoping() {}
		}
	}
</script>

<style lang="scss">
	.container {
		width: 750rpx;

		.bll {
			width: 86%;
			margin: 40rpx auto;

			.uni-list-cell .blue {
				color: rgba(237, 127, 48, 0.5);
			}

			.uni-list-cell {
				margin-top: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 24rpx;
			}

			.buttn {
				display: flex;
				margin-top: 80rpx;
				font-weight: bolder;

				.Gologin {
					width: 300rpx;
					border-radius: 50rpx;
					color: white;
					background-image: linear-gradient(to right, rgba(237, 127, 48, 0.7), rgba(236, 96, 45, 1));
				}

				.GoBack {
					width: 300rpx;
					border-radius: 50rpx;
					color: #737373;
					background-color: white;
					border: 1px solid #646464;
				}

				button::after {
					border: none;
				}
			}

			.select {
				font-size: 26rpx;
				color: #7d7d7d;
				line-height: 60rpx;
			}

			.black {
				font-size: 22rpx;
				color: black;
			}
		}

		.line {
			width: 95%;
			margin: 0 auto;
			height: 1px;
			background-color: #e8e8e8;
		}

		.logo {
			display: block;
			width: 160rpx;
			height: 160rpx;
			margin: 150rpx auto 50rpx auto;
		}
	}
</style>
